package network;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.simpay.cashierapptest.DialogScreen;
import com.simpay.cashierapptest.LoginActivity;
import lib.Core;
import lib.XmlPullParser;
import ru.qiwi.security.MD5;
import Core.Contants;
import Core.GlobalData;

public class NetworkThread extends Thread {

	private CreateXMLProvider createXMLProvider;
	private ParseXMLProvider parseXMLProvider = new ParseXMLProvider();
	private String stringUrl = "http://gate.qiwi.asia/term2/xml.ashx";

	@Override
	public void run() {
		super.run();
		HttpURLConnection hc = null;
		InputStream in = null;
		OutputStream out = null;

		try {
			createXMLProvider = new CreateXMLProvider(GlobalData.client);
			String message = createXMLProvider.toXML();

			System.out.println("request" + message);
			URL url = new URL(stringUrl);
			hc = (HttpURLConnection) url.openConnection();
			hc.setDoInput(true);
			hc.setDoOutput(true);
			hc.setUseCaches(false);
			hc.setRequestProperty("Content-Type", "text/xml");
			hc.setRequestProperty("Content-Length",
					String.valueOf(message.length()));

			hc.setRequestProperty("X-Digital-Sign",
					MD5.hashAsHex(GlobalData.auth.getSign()));//
			hc.setRequestProperty("X-Digital-Sign-Alg",
					GlobalData.auth.getSignAlg());
			hc.setRequestProperty("X-Digital-Sign-Login",
					GlobalData.auth.getLogin());

			out = hc.getOutputStream();
			out.write(message.getBytes("UTF-8"));
			in = hc.getInputStream();
			//
			byte[] b = GlobalData.log2(in);
			System.out.println("response" + new String(b));
			InputStream is = new ByteArrayInputStream(b);
			//

			if (hc.getResponseCode() == 200) {
				XmlPullParser xml = new XmlPullParser(new InputStreamReader(is,
						"UTF-8"));
				try {
					final int resultCode = parseXMLProvider
							.parseSetPublicKey(xml);

					if (LoginActivity.instanceLoginActivity != null) {
						LoginActivity.instanceLoginActivity
								.runOnUiThread(new Runnable() {

									@Override
									public void run() {
										if (resultCode == 0) {
											Core.writeData(
													Contants.ISFIRSTSTART,
													false);
											DialogScreen
													.show("Set Public key success");

										} else if (resultCode == 150) {
											DialogScreen
													.show("Lỗi #150: Thông tin đăng nhập không chính xác");

										} else if (resultCode == 151) {
											DialogScreen
													.show("Lỗi #151: Mật khẩu đăng nhập một lần");

										}

									}
								});
					}

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
				if (hc != null) {
					hc.disconnect();
				}
			} catch (IOException e) {
			}

		}

	}
}
